<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

$db = new DB('news');

$title_id = isset($_POST['title_id']) ? h($_POST['title_id']) : '';
$category_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$url = isset($_POST['url']) ? h($_POST['url']) : '';
$edit_uid = Sessions::get('uid');
$updated = time();

//检验必要参数
if( $category_id <= 0 || $title == '' || $url == '' || substr($url, 0, 7) != 'http://' ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//解析得到原文章的title_id
$empty_id = '1';
if( substr($url, 0, 21) == 'http://news.zgqw.com/' ){
	$this_url = substr($url, 21);
	//   1/25/25-2270/    article.php?title_id=30-10&page=2
	$this_url_array = explode($this_url, 'title_id=');
	
	if( count($this_url_array) == 2 ){
		$title_id_array = explode($this_url_array[1], '&');
		$empty_id = $title_id_array[0];
	}else{
		preg_match('/^(\d+\/)+(\d+-\d+)(-\d+){0,1}\/{0,1}$/', $this_url, $title_id_array);
		$empty_id = isset($title_id_array[2]) ? $title_id_array[2] : '1';
	}
}

//组装news的update数组
$news_data=array('title_id'=>$title_id,
				'title'=>$title,
				'url'=>$url,
				'empty_id'=>$empty_id,
				'edit_uid'=>$edit_uid,
				'updated'=>$updated,
				);
$db->table('news')->where('title_id="'.$title_id.'"')->update($news_data)->run();//更新

//记录动作日志
Log::logs_articles($category_id, 15, $title_id, $title);

$error_title = '编辑成功 - 编辑空白文章';
$error_content = '编辑成功！';
$error_pre_button = '<a href="/admin/news/news_lists.php?category_id='.$category_id.'" style="margin-right:20px;" id="pre_button">〈〈 返回列表</a>';
$error_next_button = '<a href="/admin/news/empty_edit.php?title_id='.$title_id.'&empty_id='.$empty_id.'">继续编辑 〉〉</a>';
require template('admin/news/news_error');
exit;